home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / OWLSRC.PAK / WSKERR.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  7KB  |  243 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1995, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   10.17  $
  6. //
  7. // Winsock for OWL subsystem.
  8. // Based on work by Paul Pedriana, 70541.3223@compuserve.com
  9. //----------------------------------------------------------------------------
  10. #include <owl/pch.h>
  11. #if !defined(OWL_MODULE_H)
  12. # include <owl/module.h>
  13. #endif
  14. #if !defined(OWL_WINSOCK_H)
  15. # include <owl/winsock.h>
  16. #endif
  17. #if !defined(OWL_WINSOCK_RH)
  18. # include <owl/winsock.rh>
  19. #endif
  20. #include <stdio.h>
  21.  
  22. OWL_DIAGINFO;
  23.  
  24. extern HINSTANCE _hInstance;   //Defined by startup code for windows programs.
  25.  
  26. //
  27. // Construct with the error code and the size of the buffer to allocate.
  28. //
  29. TSocketError::TSocketError(int error, int sizeToAllocate)
  30. :
  31.   Error(error),
  32.   String(0),
  33.   SizeToAllocate(sizeToAllocate)
  34. {
  35.   GetErrorString();
  36. }
  37.  
  38. //
  39. // Destroy the allocated string.
  40. //
  41. TSocketError::~TSocketError()
  42. {
  43.   delete[] String;
  44. }
  45.  
  46. //
  47. // Copies the error code and string.
  48. //
  49. TSocketError& TSocketError::operator =(const TSocketError& src)
  50. {
  51.   Error = src.Error;
  52.  
  53.   delete[] String;
  54.   String = strnewdup(src.String);
  55.  
  56.   return *this;
  57. }
  58.  
  59. #if defined(BI_NAMESPACE)
  60. namespace OWL {
  61. #endif
  62.  
  63. //
  64. // The important criteria for determining equality is the error value.  The
  65. //  string is unimportant.
  66. //
  67. bool operator ==(const TSocketError& socketError1,
  68.                  const TSocketError& socketError2)
  69. {
  70.   return socketError1.Error == socketError2.Error;
  71. }
  72.  
  73. #if defined(BI_NAMESPACE)
  74. } // namespace OWL
  75. #endif
  76.  
  77. //
  78. // Initialize the error code.
  79. //
  80. void TSocketError::Init(int error)
  81. {
  82.   Error = error;
  83.   GetErrorString();
  84. }
  85.  
  86. //
  87. // Return the error code.
  88. //
  89. int TSocketError::GetReasonValue() const
  90. {
  91.   return Error;
  92. }
  93.  
  94. //
  95. // This function simply hands the pointer to the string to the caller.
  96. // The caller should not mess with this string, as it doesn't belong to him.
  97. //
  98. const char* TSocketError::GetReasonString() const
  99. {
  100.   return String;
  101. }
  102.  
  103. //
  104. // This function appends an error string to whatever is in the string
  105. // 'stringToAppendErrorTo' and put the result in 'destination'.  You may
  106. // want to put something specific about the error in the string and then use
  107. // AppendError() to add the Winsock error code and description to it.
  108. //
  109. // For example, you could say:
  110. //   MessageBox(TSocketError(WSAENOTCONN).AppendError("Unable to send your mail"),
  111. //                                                   "Error", MB_OK);
  112. // And AppendError() will put "\n\nWinsock Error 10057: Socket is not presently connected"
  113. // after the "Unable to send you mail" string.  Quite convenient.
  114. // szStringToAppendErrorTo must be able to hold at least 128 characters.
  115. //
  116. char* TSocketError::AppendError(char* stringToAppendErrorTo, char* destination)
  117. {
  118.   // If no destination, then the user wants to use our own string space.
  119.   //
  120.   if (!destination) {
  121.     TAPointer<char> tempString = new char[SizeToAllocate];
  122.     strcpy(tempString, stringToAppendErrorTo);
  123.     strcat(tempString, "\n\n");
  124.     strcat(tempString, GetReasonString());
  125.     return tempString;
  126.   }
  127.  
  128.   strcpy(destination, stringToAppendErrorTo);
  129.   strcat(destination, "\n\n");
  130.   strcat(destination, GetReasonString());
  131.   return destination;
  132. }
  133.  
  134. //
  135. // This function is similar to AppendError(char*), but the pre-string is taken from
  136. //  a string resource and 'szStringToAppendErrorTo' will be overwritten with what is
  137. //  in the string resource and appended with the Winsock Error description.
  138. //
  139. // stringToAppendErrorTo must be able to hold at least 128 characters.
  140. //
  141. char* TSocketError::AppendError(int stringResourceToAppendErrorTo, char* destination)
  142. {
  143.   if (!destination) { // In this case, the user wants to use our own string.
  144.     TAPointer<char> tempString = new char[SizeToAllocate];
  145.     GetModule().LoadString(stringResourceToAppendErrorTo, tempString, 90);
  146.     AppendError(tempString);
  147.     strcpy(String, tempString);
  148.     return String;
  149.    }
  150.    GetModule().LoadString(stringResourceToAppendErrorTo, destination, 90);
  151.    return AppendError(destination);
  152. }
  153.  
  154. //
  155. // This function gets a string, suitable for display, based on the nError value.
  156. // The previous string is deleted if necessary.
  157. // Note that the string allocated is ALWAYS at least 128 characters long.
  158. // Note that even though the error strings you see below don't have
  159. //  error numbers associated with them, the function pre-pends the error number
  160. //  to the szString before returning.  If you are writing string resources
  161. //  for the error strings, don't put error numbers in the string, since we
  162. //  do that for you below.
  163. //
  164. void TSocketError::GetErrorString()
  165. {
  166.   delete[] String;
  167.   String = new char[SizeToAllocate];
  168.   String[0] = 0;
  169.  
  170.   int resIdLookup[] = {
  171.     WSAEINTR,
  172.     WSAEBADF,
  173.     WSAEACCES,
  174.     WSAEFAULT,
  175.     WSAEINVAL,
  176.     WSAEMFILE,
  177.     WSAEWOULDBLOCK,
  178.     WSAEINPROGRESS,
  179.     WSAEALREADY,
  180.     WSAENOTSOCK,
  181.     WSAEDESTADDRREQ,
  182.     WSAEMSGSIZE,
  183.     WSAEPROTOTYPE,
  184.     WSAENOPROTOOPT,
  185.     WSAEPROTONOSUPPORT,
  186.     WSAESOCKTNOSUPPORT,
  187.     WSAEOPNOTSUPP,
  188.     WSAEPFNOSUPPORT,
  189.     WSAEAFNOSUPPORT,
  190.     WSAEADDRINUSE,
  191.     WSAEADDRNOTAVAIL,
  192.     WSAENETDOWN,
  193.     WSAENETUNREACH,
  194.     WSAENETRESET,
  195.     WSAECONNABORTED,
  196.     WSAECONNRESET,
  197.     WSAENOBUFS,
  198.     WSAEISCONN,
  199.     WSAENOTCONN,
  200.     WSAESHUTDOWN,
  201.     WSAETOOMANYREFS,
  202.     WSAETIMEDOUT,
  203.     WSAECONNREFUSED,
  204.     WSAELOOP,
  205.     WSAENAMETOOLONG,
  206.     WSAEHOSTDOWN,
  207.     WSAEHOSTUNREACH,
  208.     WSAENOTEMPTY,
  209.     WSAEPROCLIM,
  210.     WSAEUSERS,
  211.     WSAEDQUOT,
  212.     WSAESTALE,
  213.     WSAEREMOTE,
  214.     WSAEDISCON,
  215.     WSASYSNOTREADY,
  216.     WSAVERNOTSUPPORTED,
  217.     WSANOTINITIALISED,
  218.     WSAHOST_NOT_FOUND,
  219.     WSATRY_AGAIN,
  220.     WSANO_RECOVERY,
  221.     WSANO_DATA,
  222.   };
  223.   int id = 0;
  224.   while (id < COUNTOF(resIdLookup) && resIdLookup[id] != Error)
  225.     id++;
  226.   TAPointer<char> temp = new char[SizeToAllocate];
  227.   GetModule().LoadString(IDS_WINSOCK_BASE+id+1, temp, SizeToAllocate);
  228.   sprintf(String, "Winsock Error %d %s", Error, (char*)temp);
  229. }
  230.  
  231. //
  232. // Return the current module.  Used when loading strings.
  233. // Important when linking to OWL dynamically because ::LoadString
  234. // would try to load from the EXE, but the WinSock resource
  235. // strings are in the DLL.
  236. //
  237. TModule&
  238. TSocketError::GetModule()         // this is a static function
  239. {
  240.   return *Module;
  241. }
  242.  
  243.